package the.guanzhong.express.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import the.broccoli.setting.service.IParamsService;
import the.guanzhong.express.infrastructure.common.enums.CargoTypeEnum;
import the.guanzhong.express.interfaces.controller.req.CargoListReq;
import the.guanzhong.express.interfaces.controller.resp.ShopBetweenResp;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 货物信息服务类
 */
@Slf4j
@Service
public class ExpressCargoService {

    @Resource
    private IParamsService paramsService;

    /**
     * 查询获取列表
     * @param req   查询条件
     * @return
     */
    public Map<String, ShopBetweenResp> getCargoList(CargoListReq req) {
        Map<String, ShopBetweenResp> data = null;
        String content = paramsService.getValue("EXPRESS_CARGO_V2");
        if (StringUtils.hasText(content)) {
            data = JSON.parseObject(content, new TypeReference<Map<String, ShopBetweenResp>>() {});
        }
        if (CollectionUtils.isEmpty(data)) {
            return null;
        }


//        for (Map.Entry<String, ShopBetweenResp> entry : data.entrySet()) {
//            ShopBetweenResp.SpecInfo specInfo2 = new ShopBetweenResp.SpecInfo();
//            specInfo2.setId(14);
//            specInfo2.setName("晚包-大包");
//
//            specInfo.setId(15);
//            specInfo.setName("晚包-小包");
//
//            if (entry.getKey().contains("102")) {
//                specInfo2.setPrice(BigDecimal.valueOf(20));
//                specInfo.setPrice(BigDecimal.valueOf(20));
//            } else {
//                specInfo2.setPrice(BigDecimal.valueOf(30));
//                specInfo.setPrice(BigDecimal.valueOf(20));
//            }
//
//            ShopBetweenResp.CargoInfo cargoInfo = new ShopBetweenResp.CargoInfo();
//            cargoInfo.setType(3);
//            cargoInfo.setDeliFee(BigDecimal.valueOf(10));
//            cargoInfo.setSpec(Lists.newArrayList(specInfo2, specInfo));
//            cargoInfo.setPromotion(new ShopBetweenResp.PromotionInfo());
//
//            entry.getValue().getCargo().add(1, cargoInfo);
//        }
//        String s = JSON.toJSONString(data);
//        log.info(s);


//        ShopBetweenResp.PromotionInfo promotion = new ShopBetweenResp.PromotionInfo();
//        List<OrderDO> orderList = expressOrderMapper.selectList(new LambdaQueryWrapper<OrderDO>()
//                .eq(OrderDO::getUserId, req.getUserId())
//                .in(OrderDO::getOrderStatus, OrderStatusEnum.WAITING_RECEIVE.getCode(), OrderStatusEnum.COMPLETED.getCode())
//                .ge(OrderDO::getOrderTime, LocalDate.now())
//                .lt(OrderDO::getOrderTime, LocalDate.now().plusDays(1))
//        );
//        if (!CollectionUtils.isEmpty(orderList)) {
//            for (OrderDO order : orderList) {
//                if (StringUtils.hasText(order.getGoodsInfo())) {
//                    List<GoodsInfoDO> goodsInfoList = JSON.parseArray(order.getGoodsInfo(), GoodsInfoDO.class);
//                    if (!CollectionUtils.isEmpty(goodsInfoList)
//                            && goodsInfoList.stream().anyMatch(it -> CargoTypeEnum.PACKET.getCode().equals(it.getCargoType()))) {
//                        promotion.setOnSale(true);
//                        promotion.setDiscount(BigDecimal.valueOf(0.5));
//                        promotion.setDesc("当天有包享受5折");
//                        break;
//                    }
//                }
//            }
//        }
//
//        if (promotion.getOnSale()) {
//            for (Map.Entry<String, ShopBetweenResp> entry : data.entrySet()) {
//                ShopBetweenResp shopBetween = entry.getValue();
//                for (ShopBetweenResp.CargoInfo cargo : shopBetween.getCargo()) {
//                    if (CargoTypeEnum.DRESS.getCode().equals(cargo.getType())) {
//                        cargo.setPromotion(promotion);
//                    }
//                }
//            }
//        }

        return data;
    }

    /**
     * 获取货物信息
     * @return
     */
    public String getCargoStr() {
        Map<String, ShopBetweenResp> data = null;
        String content = paramsService.getValue("EXPRESS_CARGO_LINE");
        if (StringUtils.hasText(content)) {
            data = JSON.parseObject(content, new TypeReference<Map<String, ShopBetweenResp>>() {});
        }
        if (CollectionUtils.isEmpty(data)) {
            return null;
        }

        for (Map.Entry<String, ShopBetweenResp> entry : data.entrySet()) {
            entry.getValue().setCargo(getCargoList());
            entry.getValue().getCargo().stream().filter(it -> CargoTypeEnum.EVENING_PACKET.getCode().equals(it.getType())).findFirst().get().setPromotion(new ShopBetweenResp.PromotionInfo());

            if (entry.getKey().contains("basic")) {
                continue;
            }

            entry.getValue().getCargo().stream().filter(it -> CargoTypeEnum.DRESS.getCode().equals(it.getType())).findFirst().get().setPromotion(getDressPromotion());

            if (entry.getKey().endsWith("201")) {
                ShopBetweenResp.PromotionInfo promotion1 = new ShopBetweenResp.PromotionInfo();
                promotion1.setType(1);
                promotion1.setOnSale(true);
                promotion1.setDiscount(BigDecimal.valueOf(0.5));
                promotion1.setDesc("下单立享5折");
                entry.getValue().getCargo().stream().filter(it -> CargoTypeEnum.PACKET.getCode().equals(it.getType())).findFirst().get().setPromotion(promotion1);
            } else {
                ShopBetweenResp.PromotionInfo promotion2 = new ShopBetweenResp.PromotionInfo();
                promotion2.setType(2);
                promotion2.setOnSale(true);
                promotion2.setThreshold(BigDecimal.valueOf(30));
                promotion2.setReduction(BigDecimal.valueOf(10));
                promotion2.setDesc("满30立减10元");
                entry.getValue().getCargo().stream().filter(it -> CargoTypeEnum.PACKET.getCode().equals(it.getType())).findFirst().get().setPromotion(promotion2);
            }
        }
        String str = JSON.toJSONString(data);
        return str;
    }

    /**
     * 货物活动
     * @return
     */
    private ShopBetweenResp.PromotionInfo getDressPromotion() {
        ShopBetweenResp.PromotionInfo dressPromotion = new ShopBetweenResp.PromotionInfo();
        dressPromotion.setOnSale(true);
        dressPromotion.setDiscount(BigDecimal.valueOf(0.5));
        dressPromotion.setType(1);
        dressPromotion.setDesc("下单立享5折");
        return dressPromotion;
    }

    /**
     * 货物类型列表
     * @return
     */
    public List<ShopBetweenResp.CargoInfo> getCargoList() {
        List<ShopBetweenResp.CargoInfo> cargoList = new ArrayList<>();

        ShopBetweenResp.CargoInfo packetCargo = new ShopBetweenResp.CargoInfo();
        packetCargo.setDeliFee(BigDecimal.valueOf(15));
        packetCargo.setType(1);
        packetCargo.setBasePrice(BigDecimal.valueOf(20));

        List<ShopBetweenResp.SpecInfo> cargoSpecList = new ArrayList<>();
        ShopBetweenResp.SpecInfo spec1 = new ShopBetweenResp.SpecInfo();
        spec1.setId(10);
        spec1.setName("大包");
        spec1.setPrice(BigDecimal.valueOf(40));
        spec1.setTakingFee(BigDecimal.valueOf(10));
        cargoSpecList.add(spec1);

        ShopBetweenResp.SpecInfo spec2 = new ShopBetweenResp.SpecInfo();
        spec2.setId(11);
        spec2.setName("中包");
        spec2.setPrice(BigDecimal.valueOf(30));
        spec2.setTakingFee(BigDecimal.valueOf(10));
        cargoSpecList.add(spec2);

        ShopBetweenResp.SpecInfo spec3 = new ShopBetweenResp.SpecInfo();
        spec3.setId(12);
        spec3.setName("小包");
        spec3.setPrice(BigDecimal.valueOf(20));
        spec3.setTakingFee(BigDecimal.ZERO);
        cargoSpecList.add(spec3);

        packetCargo.setSpec(cargoSpecList);
        cargoList.add(packetCargo);


        ShopBetweenResp.CargoInfo eveningPacketCargo = new ShopBetweenResp.CargoInfo();
        eveningPacketCargo.setDeliFee(BigDecimal.valueOf(10));
        eveningPacketCargo.setType(CargoTypeEnum.EVENING_PACKET.getCode());
        eveningPacketCargo.setBasePrice(BigDecimal.valueOf(20));

        List<ShopBetweenResp.SpecInfo> eveningPacketCargoSpecList = new ArrayList<>();
        ShopBetweenResp.SpecInfo spec4 = new ShopBetweenResp.SpecInfo();
        spec4.setId(14);
        spec4.setName("晚包-大包");
        spec4.setPrice(BigDecimal.valueOf(30));
        spec4.setTakingFee(BigDecimal.ZERO);
        eveningPacketCargoSpecList.add(spec4);

        ShopBetweenResp.SpecInfo spec5 = new ShopBetweenResp.SpecInfo();
        spec5.setId(15);
        spec5.setName("晚包-小包");
        spec5.setPrice(BigDecimal.valueOf(20));
        spec5.setTakingFee(BigDecimal.ZERO);
        eveningPacketCargoSpecList.add(spec5);

        eveningPacketCargo.setSpec(eveningPacketCargoSpecList);
        cargoList.add(eveningPacketCargo);



        ShopBetweenResp.CargoInfo dressCargo = new ShopBetweenResp.CargoInfo();
        dressCargo.setDeliFee(BigDecimal.valueOf(0));
        dressCargo.setType(CargoTypeEnum.DRESS.getCode());
        dressCargo.setBasePrice(BigDecimal.valueOf(10));

        List<ShopBetweenResp.SpecInfo> dressCargoSpecList = new ArrayList<>();
        ShopBetweenResp.SpecInfo spec6 = new ShopBetweenResp.SpecInfo();
        spec6.setId(13);
        spec6.setName("样衣");
        spec6.setPrice(BigDecimal.valueOf(20));
        spec6.setTakingFee(BigDecimal.ZERO);
        dressCargoSpecList.add(spec6);

        dressCargo.setSpec(dressCargoSpecList);
        cargoList.add(dressCargo);

        return cargoList;
    }
}
